<?php

namespace common\widgets;

use yii\base\Widget;
use yii\bootstrap\Modal;
use yii\helpers\Html;
use Yii;
use yii\web\View;
use yii\helpers\ArrayHelper;

class AjaxModal extends Widget
{

    public $url;
    public $toggleButton    = ['label' => "", "class" => "btn btn-primary"];
    public $divideToBodyEnd = false;
    public $redirectUrl     = "";
    public $refresh         = false;
    public $size            = "";

    public function init()
    {
        parent::init();
        $this->renderBtn();
    }

    protected function renderToggleButton()
    {
        if (($toggleButton = $this->toggleButton) !== false)
        {
            $tag = ArrayHelper::remove($toggleButton, 'tag', 'button');
            $label = ArrayHelper::remove($toggleButton, 'label', 'Show');
            if ($tag === 'button' && ! isset($toggleButton['type']))
            {
                $toggleButton['type'] = 'button';
            }

            return Html::tag($tag, $label, $toggleButton);
        }
        else
        {
            return null;
        }
    }

    public function renderBtn()
    {

        $modalId = "modal-" . crc32(microtime());
        if ($this->divideToBodyEnd)
        {

            if ($this->toggleButton !== false)
            {
                $this->toggleButton = array_merge([
                    'data-toggle' => 'modal',
                ], $this->toggleButton);
                if ( ! isset($this->toggleButton['data-target']) && ! isset($this->toggleButton['href']))
                {
                    $this->toggleButton['data-target'] = '#' . $modalId;
                }
            }

            echo $this->renderToggleButton();
            $this->view->on(View::EVENT_END_BODY, function ($event) use ($modalId)
            {
                $this->_renderModalBtn($modalId);
            });
        }
        else
        {
            $this->_renderModalBtn($modalId);
        }

    }

    private function _renderModalBtn($modalId)
    {
        Modal::begin([
            "id"           => $modalId,
            'header'       => "<h3>Loading...</h3>",
            'toggleButton' => $this->divideToBodyEnd ? false : $this->toggleButton,
            'size'         => $this->size,
            'footer'       => Html::button("关闭", ["class" => "btn btn-default", "data-dismiss" => "modal"]) .
                Html::button("确定", ["class" => "btn btn-primary"]),
        ]);
        $redirectUrl = $this->refresh ? "location.href" : ($this->redirectUrl ? $this->redirectUrl : "");
        echo "Loading....";
        Modal::end();
        $this->view->registerJs("
$('#{$modalId}').on('show.bs.modal', function (e) {
    $.get('{$this->url}',function(data){
        $('#{$modalId}').html(data);
    });
});"
        );
        if ($redirectUrl)
        {
            $this->view->registerJs("
$('#{$modalId}').on('hidden.bs.modal', function (e) {
    location.replace({$redirectUrl});
});
            ");
        }
    }

    static public function createModal($config = ['title' => "", "content" => ""])
    {
        $style = "";
        $config["size"] = isset($config['size']) ? $config['size'] : "";
        if ($config['size'] === Modal::SIZE_LARGE)
        {
            $style = "width:80%;";
        }
        if ($config['size'] === Modal::SIZE_SMALL)
        {
            $style = "width:30%;";
        }
        $html = <<<EOF
<div class="modal-dialog {$config["size"]}" role="document" style="{$style}">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">{$config['title']}</h4>
            </div>
            <div class="modal-body" st>
                <div>
                {$config['content']}
                </div>
            </div>
            <div class="modal-footer">
                <!--<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>-->
                <button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
            </div>
        </div>
    </div>
EOF;

        return $html;
    }

}
